import Vue from 'vue'
import VueRouter from 'vue-router'
import login from "../components/login.vue"  //导入路由login.vue
import home from "../components/home.vue"
import welcome from "../components/welcome.vue"
import users from "../components/user/users.vue"
import Rights from "../components/quanxian/Rights.vue"  //导入权限列表
import Roles from "../components/quanxian/Roles.vue"     //导入角色列表

import cate from "../components/spguanli/Cate.vue"        //导入商品管理
import params from "../components/spguanli/params.vue"    //导入分类参数
import goods from '../components/spguanli/goods.vue'      //商品列表
import Add from '../components/spguanli/Add.vue'
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/", redirect: "/login"  //redirect 重定向
  },
  {
    path: "/login", component: login
  },
  {
    path: "/home", component: home,
    redirect: "/welcome", //redirect 重定向
    children: [
      { path: "/welcome", component: welcome },
      { path: "/users", component: users },
      { path: "/rights", component: Rights }, //权限列表
      { path: "/Roles", component: Roles },    //角色列表
      { path: "/categories", component: cate },
      { path: "/params", component: params },
      { path: "/goods", component: goods },
      { path: "/goods/Add", component: Add },
      { path: "/orders", component: Order },
      { path: "/reports", component: Report}
    ]
  }
]

// 路由导航守卫控制访问权限
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  //如果用户访问的登录页，直接放行
  if (to.path === '/login') return next();

  //从sessionStorage中获取到保存的token 值
  //从会话存储中获取到保存的令牌值
  const tokenStr = window.sessionStorage.getItem('token')

  //没有token,强制跳转到登录页
  //没有令牌，强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
export default router
